package net.zoneland.knowledge.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.service.ErrorCorrectionService;
import net.zoneland.knowledge.utils.LoginUtils;

/**
 * 纠错控制器.
 * @author zmq
 * @date 2021/10/8 16:20
 */
@RestController
@RequestMapping("/ec")
public class ErrorCorrectionController {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ErrorCorrectionController.class);
    /**
     * 纠错业务接口.
     */
    @Autowired
    private ErrorCorrectionService errorCorrectionService;

    /**
     * 添加纠错.
     * @param request http请求
     * @param desc 描述
     * @param esQuestionId es题库id
     * @param files 附件
     * @return 添加结果
     */
    @ResponseBody
    @PostMapping("/addCorrect")
    public ResponseResult addCorrect(final HttpServletRequest request,
                                     final String desc, final String esQuestionId,
                                     @RequestBody final List<MultipartFile> files) {
        ResponseResult result = new ResponseResult();
        try {
            LoginUserPojo user = LoginUtils.getCurrentUserByToken(request.getHeader(Constants.TOKEN_NAME));
            errorCorrectionService.addErrorCorrection(desc, esQuestionId, user, files);
        } catch (Exception e) {
            LOGGER.error("添加纠错失败，esQuestionId:{}, desc:{}", esQuestionId, desc, e);
            result.setResult("0");
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
